package cn.itsource.org.service.impl;

import cn.itsource.basic.domain.BaseTreeNode;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.PageList;
import cn.itsource.basic.util.TreeUtil;
import cn.itsource.org.domain.Department;
import cn.itsource.org.mapper.DepartmentMapper;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department,DepartmentQuery> implements IDepartmentService {

    @Resource
    private DepartmentMapper departmentMapper;



    @Override
    public PageList<Department> findPage(DepartmentQuery query) {
        // 创建高级查询对象

        // 设置分页
        PageHelper.startPage(query.getPage(),query.getSize());
        // 查询数据
        List<Department> page = departmentMapper.findPage(query);
        // 封装对象
        PageInfo<Department> pageInfo = new PageInfo<>(page);
        // 封装到自定义的对象中并返回
        PageList<Department> pageList = new PageList<>(pageInfo.getTotal(), pageInfo.getList());
        return pageList;
    }



    @Override
    public void saveOrUpdate(Department department) {
//        //判断名字和编号是否为空
//        if(StringUtils.isBlank(department.getName())){
//            throw new RuntimeException("部门名字没有填写");
//        }
//        if(StringUtils.isBlank(department.getSn())){
//            throw new RuntimeException("部门编号没有填写");
//        }
        if (department.getId() == null){
            departmentMapper.insertSelective(department);
            this.getDirpath(department);
        }else {
            //查询自己原来的数据
            Department department2 = departmentMapper.selectByPrimaryKey(department.getId());
            //老路径
            String oldDirPath = department2.getDirPath();
            //新路径
            String dirPathNew = this.getDirpath(department);
            //查询所有老路劲有关的数据
            List<Department> departments = departmentMapper.selectOldDirPath(oldDirPath);
            //打算替换所有id并且升级
            for (Department department1 : departments) {
                department1.setDirPath(department1.getDirPath().replace(oldDirPath,dirPathNew));
                //升级
                departmentMapper.updateByPrimaryKey(department1);
            }
        }
    }

    private String getDirpath(Department department) {
        //1.首先获取fuid集合
        Long[] parentIds = department.getParentIds();//[3,8,10]
        //判断父id的集合是否为空
        StringBuilder sb = new StringBuilder();
        if(parentIds !=null && parentIds.length>0){
            //设置自己的父ID新增的时候是不知道自己的id的
            Long parentId = parentIds[parentIds.length - 1];
            department.setParentId(parentId);
            //判断是否选择自己为上级
            ArrayList<Long> array = new ArrayList<>();
            HashSet<Long> sets = new HashSet<>();
            //设置自己的dirpath必须知道自己的id
            for (Long id : parentIds) {
                sb.append("/").append(id); // /3/8/10
                array.add(id);
                sets.add(id);
            }
            sb.append("/").append(department.getId()); //3/8/10/id
            array.add(department.getId());
            sets.add(department.getId());
            if(array.size()!=sets.size()){
                //扔出异常
                throw new RuntimeException("不能选择子目录为上级");
            }
            //放回修改的路劲
            return sb.toString();

        }else {
            sb.append("/").append(department.getId()); // /id
        }
        department.setDirPath(sb.toString());
        //再进行升级
        departmentMapper.updateByPrimaryKeySelective(department);
        return null;
    }



    @Override
    public List<Department> findParentDepts() {
        return departmentMapper.findParentDepts();
    }



    @Override
    public List<Department> deptTree() {
        List<Department> departments = departmentMapper.selectAll();
        List<Department> tree = TreeUtil.getTree(departments);
        return tree;
    }
}
